package com.mojiayi.learn.swagger;

import cn.hutool.core.io.IoUtil;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ReadSwaggerDocTest {
    public static void main(String[] args) throws IOException {
        List<SwaggerRequest> requestList = readRequestInfoFromFile();

//        outputAllRequestInfo(requestList);

        outputRequestPathAndName(requestList);
    }

    private static List<SwaggerRequest> readRequestInfoFromFile() throws IOException {
        List<String> lines = new ArrayList<>();
        IoUtil.readLines(new FileInputStream(new File("C:\\Users\\liguangri\\Downloads\\taoli-biz-web-20210604101652.json")), "UTF-8", lines);
        StringBuilder stringBuilder = new StringBuilder();
        lines.forEach(v -> {
            stringBuilder.append(v.trim().replaceAll(" ", ""));
        });
        JsonObject jsonObject = new JsonParser().parse(stringBuilder.toString()).getAsJsonObject();

//        List<String> controllerTags = new ArrayList<>();
//        jsonObject.getAsJsonArray("tags").forEach(v -> {
//            controllerTags.add(v.getAsJsonObject().get("name").getAsString().trim());
//        });

        JsonObject pathJsonObject = jsonObject.getAsJsonObject("paths");
        List<SwaggerRequest> requestList = new ArrayList<>(pathJsonObject.entrySet().size());
        SwaggerRequest swaggerRequest = null;
        for (Map.Entry<String, JsonElement> entry : pathJsonObject.entrySet()) {
            JsonObject requestJsonObject = entry.getValue().getAsJsonObject().getAsJsonObject("get");
            if (requestJsonObject == null) {
                requestJsonObject = entry.getValue().getAsJsonObject().getAsJsonObject("post");
            }
            if (requestJsonObject == null) {
                continue;
            }
            swaggerRequest = new SwaggerRequest();
            swaggerRequest.path = entry.getKey();
            swaggerRequest.summary = requestJsonObject.get("summary").getAsString();
            for (JsonElement jsonElement : requestJsonObject.getAsJsonArray("tags")) {
                String tagName = jsonElement.getAsString().trim();
//                if (controllerTags.contains(tagName)) {
//                    continue;
//                }

                if (tagName.startsWith("1级菜单")) {
                    swaggerRequest.setTagFirstLevelMenu(tagName.replace("1级菜单-", "").replace("1级菜单", ""));
                } else if (tagName.startsWith("2级菜单")) {
                    swaggerRequest.setTagSecondLevelMenu(tagName.replace("2级菜单-", "").replace("2级菜单", ""));
                } else if (tagName.startsWith("3级菜单")) {
                    swaggerRequest.setTagThirdLevelMenu(tagName.replace("3级菜单-", "").replace("3级菜单", ""));
                } else if (tagName.startsWith("1级功能")) {
                    swaggerRequest.setTagFirstLevelFunction(tagName.replace("1级功能-", "").replace("1级功能", ""));
                } else if (tagName.startsWith("2级功能")) {
                    swaggerRequest.setTagSecondLevelFunction(tagName.replace("2级功能-", "").replace("2级功能", ""));
                } else {
                    swaggerRequest.setTagModuleName(tagName);
                }
            }
            requestList.add(swaggerRequest);
        }

        requestList.sort((o1, o2) -> {
            int result = compareTwoTags(o1.getTagFirstLevelMenu(), o2.getTagFirstLevelMenu());
            if (result == 0) {
                result = compareTwoTags(o1.getTagSecondLevelMenu(), o2.getTagSecondLevelMenu());
            }
            if (result == 0) {
                result = compareTwoTags(o1.getTagThirdLevelMenu(), o2.getTagThirdLevelMenu());
            }
            if (result == 0) {
                result = compareTwoTags(o1.getTagFirstLevelFunction(), o2.getTagFirstLevelFunction());
            }
            if (result == 0) {
                result = compareTwoTags(o1.getTagSecondLevelFunction(), o2.getTagSecondLevelFunction());
            }
            return result;
        });
        return requestList;
    }

    private static void outputRequestPathAndName(List<SwaggerRequest> requestList) {
        requestList.forEach(v -> {
            System.out.print("/api/afterSales" + v.path + "\t");
            System.out.println(Optional.ofNullable(v.summary).orElse(""));
        });
    }

    private static void outputAllRequestInfo(List<SwaggerRequest> requestList) {
        requestList.forEach(v -> {
            System.out.print(Optional.ofNullable(v.tagFirstLevelMenu).orElse("") + "\t");
            System.out.print(Optional.ofNullable(v.tagSecondLevelMenu).orElse("") + "\t");
            System.out.print(Optional.ofNullable(v.tagThirdLevelMenu).orElse("") + "\t");
            System.out.print(Optional.ofNullable(v.summary).orElse("") + "\t");
            System.out.println("/api/afterSales" + v.path);
        });
    }

    private static int compareTwoTags(String tagOne, String tagTwo) {
        if (tagOne == null && tagTwo == null) {
            return 0;
        }
        if (tagOne == null) {
            return 1;
        }
        if (tagTwo == null) {
            return -1;
        }
        return tagOne.compareTo(tagTwo);
    }
}

class SwaggerRequest {
    public String path;
    public String summary;
    public String tagFirstLevelMenu;
    public String tagSecondLevelMenu;
    public String tagThirdLevelMenu;
    public String tagFirstLevelFunction;
    public String tagSecondLevelFunction;
    public String tagModuleName;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getTagFirstLevelMenu() {
        return tagFirstLevelMenu;
    }

    public void setTagFirstLevelMenu(String tagFirstLevelMenu) {
        this.tagFirstLevelMenu = tagFirstLevelMenu;
    }

    public String getTagSecondLevelMenu() {
        return tagSecondLevelMenu;
    }

    public void setTagSecondLevelMenu(String tagSecondLevelMenu) {
        this.tagSecondLevelMenu = tagSecondLevelMenu;
    }

    public String getTagThirdLevelMenu() {
        return tagThirdLevelMenu;
    }

    public void setTagThirdLevelMenu(String tagThirdLevelMenu) {
        this.tagThirdLevelMenu = tagThirdLevelMenu;
    }

    public String getTagFirstLevelFunction() {
        return tagFirstLevelFunction;
    }

    public void setTagFirstLevelFunction(String tagFirstLevelFunction) {
        this.tagFirstLevelFunction = tagFirstLevelFunction;
    }

    public String getTagSecondLevelFunction() {
        return tagSecondLevelFunction;
    }

    public void setTagSecondLevelFunction(String tagSecondLevelFunction) {
        this.tagSecondLevelFunction = tagSecondLevelFunction;
    }

    public String getTagModuleName() {
        return tagModuleName;
    }

    public void setTagModuleName(String tagModuleName) {
        this.tagModuleName = tagModuleName;
    }
}